import axios from 'axios'
import { ElMessage } from 'element-plus'

const service = axios.create({
  baseURL: process.env.VUE_APP_DOMAIN + process.env.VUE_APP_BAS_API,
  timeout: 5000
})

// 请求拦截器
service.interceptors.request.use((config) => {
  // 设置header
  config.headers.Authorization = localStorage.getItem('token')
  return config
}, error => {
  return Promise.reject(new Error(error.message))
})

// 响应拦截器
service.interceptors.response.use((resp) => {
  console.log(resp)
  const {
    data,
    message,
    code
  } = resp.data
  if (code === 0) {
    return data
  } else {
    ElMessage.error('请求发生异常')
    return Promise.reject(new Error(message))
  }
}, (error) => {
  ElMessage.error(error.message)
  return Promise.reject(new Error(error.message))
})
export default service
